/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ywb.edugui.view.teacher;

import com.ywb.edugui.controller.ControllerFactory;
import com.ywb.edugui.controller.TeacherController;
import com.ywb.edugui.entity.Teacher;
import com.ywb.edugui.utils.ImageUtils;
import lombok.Data;

import java.awt.image.BufferedImage;
import java.util.ResourceBundle;


/**
 * 添加、修改教师面板、使用内置techer属性判断新增还是修改
 * 调用无参构造的时候是添加
 * 调用有参构造的时候是修改
 *
 * @author 10147
 */
@Data
public class TeacherAddOrUpdatePanel extends javax.swing.JPanel {
    private TeacherController teacherController = ControllerFactory.getTeacherController(this);
//    被选择的文件
    private String selectedFileName = "";
    private BufferedImage avatarImage = null;
//    用于接收需要更新的教师,以判断按钮是更新还是删除
    private Teacher updateTeacher = null;
    private boolean uploadFlag = false;

    /**
     * Creates new form TeacherAddOrUpdatePanel
     */
    public TeacherAddOrUpdatePanel() {
        initComponents();
    }
    public TeacherAddOrUpdatePanel(Teacher teacher){
        this();
        updateTeacher = teacher;
        dataShowBack();
        jbAddOrUpdateTeacher.setText("修改教师");
    }

    /**
     * 数据回显
     */
    private void dataShowBack() {
        tfTeacherIntro.setText(updateTeacher.getIntro());
        tfTeacherName.setText(updateTeacher.getName());
        cbTeacherLevel.setSelectedIndex(updateTeacher.getLevel() == 1 ? 1 : 2);
        jlAvatar.setIcon(ImageUtils.urlToImageIcon(updateTeacher.getAvatar()));
    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jlTeacherName = new javax.swing.JLabel();
        jlTeacherIntro = new javax.swing.JLabel();
        jlTeacherTitle = new javax.swing.JLabel();
        jlTeacherAvatar = new javax.swing.JLabel();
        tfTeacherName = new javax.swing.JTextField();
        tfTeacherIntro = new javax.swing.JTextField();
        cbTeacherLevel = new javax.swing.JComboBox<>();
        jlAvatar = new javax.swing.JLabel();
        jbAddOrUpdateTeacher = new javax.swing.JButton();
        jbAvatar = new javax.swing.JButton();

        setLayout(null);

        jlTeacherName.setText("教师姓名");
        add(jlTeacherName);
        jlTeacherName.setBounds(130, 80, 80, 15);

        jlTeacherIntro.setText("教师简介");
        add(jlTeacherIntro);
        jlTeacherIntro.setBounds(130, 180, 80, 15);

        jlTeacherTitle.setText("教师职称");
        add(jlTeacherTitle);
        jlTeacherTitle.setBounds(130, 130, 80, 15);

        jlTeacherAvatar.setText("教师头像");
        add(jlTeacherAvatar);
        jlTeacherAvatar.setBounds(130, 240, 80, 15);

        tfTeacherName.setToolTipText("E.g. 张三");
        tfTeacherName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfTeacherNameActionPerformed(evt);
            }
        });
        add(tfTeacherName);
        tfTeacherName.setBounds(240, 80, 320, 40);

        tfTeacherIntro.setToolTipText("E.g. 毕业于.....");
        tfTeacherIntro.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tfTeacherIntroActionPerformed(evt);
            }
        });
        add(tfTeacherIntro);
        tfTeacherIntro.setBounds(240, 170, 320, 40);

        cbTeacherLevel.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "", "初级教师", "高级教师"}));
        cbTeacherLevel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbTeacherLevelActionPerformed(evt);
            }
        });
        add(cbTeacherLevel);
        cbTeacherLevel.setBounds(240, 130, 320, 30);
        add(jlAvatar);
        jlAvatar.setBounds(240, 220, 100, 100);

        jbAddOrUpdateTeacher.setText("添加");
        jbAddOrUpdateTeacher.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAddTeacherActionPerformed(evt);
            }
        });
        add(jbAddOrUpdateTeacher);
        jbAddOrUpdateTeacher.setBounds(130, 330, 440, 40);

        jbAvatar.setText("上传");
        jbAvatar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbAvatarActionPerformed(evt);
            }
        });
        add(jbAvatar);
        jbAvatar.setBounds(390, 240, 81, 23);
    }// </editor-fold>


    private void tfTeacherNameActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    private void tfTeacherIntroActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    private void cbTeacherLevelActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    private void jbAddTeacherActionPerformed(java.awt.event.ActionEvent evt) {
        saveOrUpdate();
    }
    private void saveOrUpdate(){
        teacherController.saveOrUpdate();
    }

    private void jbAvatarActionPerformed(java.awt.event.ActionEvent evt) {
        teacherController.avatarSelect();
    }



    // Variables declaration - do not modify
    private javax.swing.JComboBox<String> cbTeacherLevel;
    private javax.swing.JButton jbAddOrUpdateTeacher;
    private javax.swing.JButton jbAvatar;
    private javax.swing.JLabel jlAvatar;
    private javax.swing.JLabel jlTeacherAvatar;
    private javax.swing.JLabel jlTeacherIntro;
    private javax.swing.JLabel jlTeacherName;
    private javax.swing.JLabel jlTeacherTitle;
    private javax.swing.JTextField tfTeacherIntro;
    private javax.swing.JTextField tfTeacherName;
    // End of variables declaration

    public void setRegionText(ResourceBundle bundle) {

        jlTeacherAvatar.setText(bundle.getString("teacher.avatar"));
        jlTeacherIntro.setText(bundle.getString("teacher.intro"));
        jlTeacherName.setText(bundle.getString("teacher.name"));
        jlTeacherTitle.setText(bundle.getString("teacher.level"));
        jbAvatar.setText(bundle.getString("teacher.upload"));
        if(isAddPanel()){
            jbAddOrUpdateTeacher.setText(bundle.getString("teacher.add"));
        }else{
            jbAddOrUpdateTeacher.setText(bundle.getString("teacher.update"));
        }
    }

    /**
     * 判断当前对象面板是新增还是修改
     * @return
     */
    public boolean isAddPanel(){
        return updateTeacher == null;
    }
    // End of variables declaration
}
